import { color } from 'cc';
import { Color } from 'cc';
import { _decorator, Component, Node, Sprite, isValid, UITransform, log, v4 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('effect_highlight_light')
export class effect_highlight_light extends Component {

    _sprite: Sprite = null;

    @property
    useMaterialInstance: boolean = false;

    onLoad() {
        this._sprite = this.getComponent(Sprite);

        this.scheduleOnce(() => {
            if (isValid(this.node)) {
                this.refreshMaterial();
            }
        }, .1);
    }

    @property
    private _rot: number = -.1;
    @property({ slide: true, range: [-3.14, 3.14, .01] })
    public get rot(): number { return this._rot; }
    public set rot(v: number) {
        this._rot = v;
        this.refreshMaterial();
    }

    @property
    private _width: number = .4;
    @property({ slide: true, range: [0, 1, .01] })
    public get width(): number { return this._width; }
    public set width(v: number) {
        this._width = v;
        this.refreshMaterial();
    }

    @property
    private _lightColor: Color = color(1, 1, 1, 1);
    @property
    public get lightColor(): Color { return this._lightColor; }
    public set lightColor(v: Color) {
        this._lightColor = v;
        this.refreshMaterial();
    }

    refreshMaterial() {
        if (this._sprite == null) this._sprite = this.node.getComponent(Sprite);
        if (this._sprite == null || this._sprite.spriteFrame == null) return;

        let material = this.useMaterialInstance ? this._sprite.getMaterialInstance(0) : this._sprite.getSharedMaterial(0);
        if (material) {
            let uvSliced: { u: number, v: number }[] = this._sprite.spriteFrame.uvSliced;
            let capInsets: number[] = (this._sprite.spriteFrame as any)._capInsets;
            // log('uvSliced', uvSliced);
            // log('capInsets', capInsets);

            const transform = this.node.getComponent(UITransform);
            let uv: number[] = this._sprite.spriteFrame.uv;
            // log('uv', uv);
            material.setProperty('lrbt', v4(uv[0], uv[6], uv[7], uv[1]));
            material.setProperty('uvs', v4(uvSliced[1].u, uvSliced[2].u, uvSliced[8].v, uvSliced[4].v));
            material.setProperty('ur', v4(0, capInsets[0], transform.width - capInsets[2], transform.width));
            material.setProperty('vr', v4(0, capInsets[1], transform.height - capInsets[3], transform.height));
            material.setProperty('lightColor', v4(this.lightColor.r / 255, this.lightColor.g / 255, this.lightColor.b / 255, this.lightColor.a / 255));

            material.setProperty('arg0', v4(this.width, this.rot, .2, 0)); // 并没有使用

            // console.log([uv[0], uv[6], uv[1], uv[7]]);
            // console.log([uvSliced[1].u, uvSliced[2].u, uvSliced[8].v, uvSliced[4].v]);
            // console.log([0, capInsets[0], transform.width - capInsets[2], transform.width]);
            // console.log([0, capInsets[1], transform.height - capInsets[3], transform.height]);

            this.useMaterialInstance ? this._sprite.setMaterialInstance(material, 0) : this._sprite.setSharedMaterial(material, 0);
        }

    }
}

